package com.xiaodai.miaosha.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaodai.miaosha.entity.SeckillOrder;
import com.xiaodai.miaosha.entity.Msuser;
import com.xiaodai.miaosha.mapper.SeckillOrderMapper;
import com.xiaodai.miaosha.service.ISeckillOrderService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 秒杀订单表 服务实现类
 * </p>
 *
 * @author xiaodai
 * @since 2022-03-20
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SeckillOrderMapper seckillOrderMapper;

    @Override
    public Long getResult(Msuser Msuser, Long goodsId,String addressId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>().eq("user_id",Msuser.getId()).eq("goods_id",goodsId).eq("delivery_addr_id",addressId));
        if (seckillOrder != null){
            System.out.println(seckillOrder.getOrderId());
            return seckillOrder.getOrderId();
        }else if (redisTemplate.hasKey("isStockEmpty:"+goodsId)){
            return -1L;
        }else {
            return 0L;
        }
    }
}
